class Solution {
public:
    bool check(vector<int>& nums, vector<vector<int>>& queries,int k)
    {
        vector<int> dis(nums.size()+1);
        dis[0]=nums[0];

        for(int i=1;i<nums.size();i++)
        {
            dis[i]=nums[i]-nums[i-1];
        }

        for(int i=0;i<=k;i++)
        {
            int l=queries[i][0],r=queries[i][1];
            int val=queries[i][2];
            dis[l]-=val;
            dis[r+1]+=val;
        }
        int cur=0;
        for(int i=0;i<nums.size();i++)
        {
            cur+=dis[i];
            if(cur>0)
            {
                return false;
            }
        }
        return true;
    }
    int minZeroArray(vector<int>& nums, vector<vector<int>>& queries) {
        int left=0,right=queries.size()-1;
        int flg=true;
        for(int i=0;i<nums.size();i++)
        {
            if(nums[i]!=0)
            {
                flg=false;
                break;
            }
        }

        if(flg)
        {
            return 0;
        }
        while(left<right)
        {
            int mid=left+(right-left)/2;
            if(check(nums,queries,mid))
            {
                right=mid;
            }
            else{
                left=mid+1;
            }
        }
        if(check(nums,queries,left))
        {
            return left+1;
        }
        else{
            return -1;
        }
    }
};
